package com.xy.xylive.xyapi;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xylink.config.SDKConfigMgr;
import com.xylink.model.*;
import com.xylink.util.HttpUtil;
import com.xylink.util.Result;
import com.xylink.util.SignatureSample;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class LiveVideoV5Api {

    private static SignatureSample signatureSample = new SignatureSample();
    private static final String prefixUrl = "/api/rest/external/v1/liveVideo5/";

    public LiveVideoV5Api() {
    }

    // new api 预约直播
    public Result<LiveVideoRes> scheduleLive(String enterpriseId, String token, String meetingRoomNumber, LiveVideoReq liveVideo) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "/subscribe?enterpriseId=" + enterpriseId + "&meetingRoomNumber=" + meetingRoomNumber;
        String jsonEntity = (new ObjectMapper()).writeValueAsString(liveVideo);
        surl = signatureSample.appendSigniture(jsonEntity, "POST", token, surl);
        return HttpUtil.getResponse(surl, "POST", jsonEntity, LiveVideoRes.class);
    }

    public Result<LiveVideoRes> updateLive(String enterpriseId, String token, String meetingRoomNumber, String liveId, LiveVideoReq liveVideo) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "/update?enterpriseId=" + enterpriseId + "&meetingRoomNumber=" + meetingRoomNumber + "&liveId="+liveId;
        String jsonEntity = (new ObjectMapper()).writeValueAsString(liveVideo);
        surl = signatureSample.appendSigniture(jsonEntity, "POST", token, surl);
        return HttpUtil.getResponse(surl, "POST", jsonEntity, LiveVideoRes.class);
    }

    public Result<LiveVideoRes> getLive(String enterpriseId, String token, String liveId, String meetingRoomNumber, String password) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "/query?enterpriseId=" + enterpriseId + "&liveId=" + liveId + "&meetingRoomNumber=" + meetingRoomNumber + "&password=" + password;
        surl = signatureSample.appendSigniture("", "GET", token, surl);
        return HttpUtil.getResponse(surl, "GET", (String)null, LiveVideoRes.class);
    }

    /**
     * 获取三方直播回放及下载信息
     */
    public Result<ThirdpartLiveVideo[]> liveVideoList(String enterpriseId, String token, String liveId, String meetingRoomNumber) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "/thirdpart/liveVideoInfo?enterpriseId=" + enterpriseId + "&liveId=" + liveId + "&meetingRoomNumber=" + meetingRoomNumber;
        surl = signatureSample.appendSigniture("", "GET", token, surl);
        return HttpUtil.getResponse(surl, "GET", null, ThirdpartLiveVideo[].class);
    }

    private String getPrefixUrl(String enterpriseId) {
        return SDKConfigMgr.getServerHost(enterpriseId) + prefixUrl;
    }
}
